package yunwo.cn.yzstation.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.okapi.RegistCall;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.RegistBean;
import yunwo.cn.yzstation.config.MyConstants;

/**
 * Created by lao on 2017/9/6.
 */
public class RegistnextActivity extends MyBaseActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener {

    private String mVerification;
    private String mUserhone;
    private RadioGroup mRadioGroup;
    private EditText mNick;
    private EditText mPaswd;
    String sex = "1";
    private TextView mSubmit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initExtra();
        initView();

    }

    private void initView() {
        mPaswd = (EditText) findViewById(R.id.paswd);
        mNick = (EditText) findViewById(R.id.nick);
        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mSubmit = (TextView) findViewById(R.id.submit);
        mSubmit.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    private void initExtra() {
        mVerification = getIntent().getStringExtra("Verification");
        mUserhone = getIntent().getStringExtra("userhone");
    }

    @Override
    protected String settiltile() {
        return "手机号注册";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_registnext;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit:
                String nick = mNick.getText().toString().trim();
                final String paswd = mPaswd.getText().toString().trim();
//                final String url = "http://119.23.230.194:8083/v1/user/registry";
                final Map<String,String> params = new HashMap<>();
                params.put("captcha",mVerification);
                params.put("gender",sex);
                params.put("mobile",mUserhone);
                params.put("nickname",nick);
                params.put("password",paswd);
                params.put("userType","");
                OkHttpUtils.post().url(MyConstants.regist).params(params).build().execute(new RegistCall() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(RegistnextActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(RegistBean response, int id) {
                        if (response.getCode() == 200) {
                            Intent intent = new Intent(RegistnextActivity.this, LoginActivity.class);
                            intent.putExtra("userphone", mUserhone);
                            intent.putExtra("pawd", paswd);
                            startActivity(intent);
                            finish();
                        }
                        else {
                            Toast.makeText(RegistnextActivity.this, response.getMsg(), Toast.LENGTH_SHORT).show();

                        }

                    }
                });


                break;

        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton rb = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());

        if (rb.getText().toString().equals("男")) {
            sex = "1";
        }else {
            sex = "2";
        }
    }
}
